package com.bee.common.tools.date;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 鏃ユ湡绫诲瀷
 *
 * @author along
 *
 */
public class DateUtils {
	public static final String TIME_WITH_MINUTE_PATTERN = "HH:mm";

	public static final long DAY_MILLI = 24 * 60 * 60 * 1000; // 涓�澶╃殑MilliSecond

	public final static int LEFT_OPEN_RIGHT_OPEN = 1;
	public final static int LEFT_CLOSE_RIGHT_OPEN = 2;
	public final static int LEFT_OPEN_RIGHT_CLOSE = 3;
	public final static int LEFT_CLOSE_RIGHT_CLOSE = 4;
	/**
	 * 姣旇緝鏃ユ湡鐨勬ā寮� --鍙瘮杈冩棩鏈燂紝涓嶆瘮杈冩椂闂�
	 */
	public final static int COMP_MODEL_DATE = 1;
	/**
	 * 姣旇緝鏃ユ湡鐨勬ā寮� --鍙瘮杈冩椂闂达紝涓嶆瘮杈冩棩鏈�
	 */
	public final static int COMP_MODEL_TIME = 2;
	/**
	 * 姣旇緝鏃ユ湡鐨勬ā寮� --姣旇緝鏃ユ湡锛屼篃姣旇緝鏃堕棿
	 */
	public final static int COMP_MODEL_DATETIME = 3;

//	private static Logger logger = Logger.getLogger(DateUtils.class);

	/**
	 * 瑕佺敤鍒扮殑DATE Format鐨勫畾涔�
	 */
	public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 骞�/鏈�/鏃�
	public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"; // 骞�/鏈�/鏃�
	public static SimpleDateFormat sdfDateTime = new SimpleDateFormat(DateUtils.DATE_FORMAT_DATETIME);
	// Global SimpleDateFormat object
	public static SimpleDateFormat sdfDateOnly = new SimpleDateFormat(DateUtils.DATE_FORMAT_DATEONLY);
	public static final SimpleDateFormat SHORTDATEFORMAT = new SimpleDateFormat("yyyyMMdd");
	public static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat LONG_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat HMS_FORMAT = new SimpleDateFormat("HH:mm:ss");
	public static final SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	/**
	 * 鏍规嵁鍗曚綅瀛楁姣旇緝涓や釜鏃ユ湡
	 *
	 * @param date
	 *            鏃ユ湡1
	 * @param otherDate
	 *            鏃ユ湡2
	 * @param withUnit
	 *            鍗曚綅瀛楁锛屼粠Calendar field鍙栧��
	 * @return 绛変簬杩斿洖0鍊�, 澶т簬杩斿洖澶т簬0鐨勫�� 灏忎簬杩斿洖灏忎簬0鐨勫��
	 */
	public static int compareDate(Date date, Date otherDate, int withUnit) {
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);
		Calendar otherDateCal = Calendar.getInstance();
		otherDateCal.setTime(otherDate);

		switch (withUnit) {
			case Calendar.YEAR:
				dateCal.clear(Calendar.MONTH);
				otherDateCal.clear(Calendar.MONTH);
			case Calendar.MONTH:
				dateCal.set(Calendar.DATE, 1);
				otherDateCal.set(Calendar.DATE, 1);
			case Calendar.DATE:
				dateCal.set(Calendar.HOUR_OF_DAY, 0);
				otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
			case Calendar.HOUR:
				dateCal.clear(Calendar.MINUTE);
				otherDateCal.clear(Calendar.MINUTE);
			case Calendar.MINUTE:
				dateCal.clear(Calendar.SECOND);
				otherDateCal.clear(Calendar.SECOND);
			case Calendar.SECOND:
				dateCal.clear(Calendar.MILLISECOND);
				otherDateCal.clear(Calendar.MILLISECOND);
			case Calendar.MILLISECOND:
				break;
			default:
				throw new IllegalArgumentException("withUnit 鍗曚綅瀛楁 " + withUnit + " 涓嶅悎娉曪紒锛�");
		}
		return dateCal.compareTo(otherDateCal);
	}

	/**
	 * 鏍规嵁鍗曚綅瀛楁姣旇緝涓や釜鏃堕棿
	 *
	 * @param date
	 *            鏃堕棿1
	 * @param otherDate
	 *            鏃堕棿2
	 * @param withUnit
	 *            鍗曚綅瀛楁锛屼粠Calendar field鍙栧��
	 * @return 绛変簬杩斿洖0鍊�, 澶т簬杩斿洖澶т簬0鐨勫�� 灏忎簬杩斿洖灏忎簬0鐨勫��
	 */
	public static int compareTime(Date date, Date otherDate, int withUnit) {
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(date);
		Calendar otherDateCal = Calendar.getInstance();
		otherDateCal.setTime(otherDate);

		dateCal.clear(Calendar.YEAR);
		dateCal.clear(Calendar.MONTH);
		dateCal.set(Calendar.DATE, 1);
		otherDateCal.clear(Calendar.YEAR);
		otherDateCal.clear(Calendar.MONTH);
		otherDateCal.set(Calendar.DATE, 1);
		switch (withUnit) {
			case Calendar.HOUR:
				dateCal.clear(Calendar.MINUTE);
				otherDateCal.clear(Calendar.MINUTE);
			case Calendar.MINUTE:
				dateCal.clear(Calendar.SECOND);
				otherDateCal.clear(Calendar.SECOND);
			case Calendar.SECOND:
				dateCal.clear(Calendar.MILLISECOND);
				otherDateCal.clear(Calendar.MILLISECOND);
			case Calendar.MILLISECOND:
				break;
			default:
				throw new IllegalArgumentException("withUnit 鍗曚綅瀛楁 " + withUnit + " 涓嶅悎娉曪紒锛�");
		}
		return dateCal.compareTo(otherDateCal);
	}

	/**
	 * 鑾峰緱褰撳墠鐨勬棩鏈熸绉�
	 *
	 * @return
	 */
	public static long nowTimeMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * 鑾峰緱褰撳墠鐨勬椂闂存埑
	 *
	 * @return
	 */
	public static Timestamp nowTimeStamp() {
		return new Timestamp(nowTimeMillis());
	}

	/**
	 * yyyy-MM-dd 褰撳墠鏃ユ湡
	 *
	 */
	public static String getReqDate() {
		return SHORTDATEFORMAT.format(new Date());
	}

	/**
	 * yyyy-MM-dd 浼犲叆鏃ユ湡
	 *
	 * @param date
	 * @return
	 */
	public static String getReqDate(Date date) {
		return SHORT_DATE_FORMAT.format(date);
	}

	/**
	 * yyyyMMdd 浼犲叆鏃ユ湡
	 *
	 * @param date
	 * @return
	 */
	public static String getReqDateyyyyMMdd(Date date) {
		return SHORTDATEFORMAT.format(date);
	}

	/**
	 * yyyy-MM-dd 浼犲叆鐨勬椂闂存埑
	 *
	 * @param date
	 * @return
	 */
	public static String TimestampToDateStr(Timestamp tmp) {
		return SHORT_DATE_FORMAT.format(tmp);
	}

	/**
	 * HH:mm:ss 褰撳墠鏃堕棿
	 *
	 * @return
	 */
	public static String getReqTime() {
		return HMS_FORMAT.format(new Date());
	}

	/**
	 * 寰楀埌鏃堕棿鎴虫牸寮忓瓧涓�
	 *
	 * @param date
	 * @return
	 */
	public static String getTimeStampStr(Date date) {
		return LONG_DATE_FORMAT.format(date);
	}

	/**
	 * 寰楀埌闀挎棩鏈熸牸寮忓瓧涓�
	 *
	 * @return
	 */
	public static String getLongDateStr() {
		return LONG_DATE_FORMAT.format(new Date());
	}

	public static String getLongDateStr(Timestamp time) {
		return LONG_DATE_FORMAT.format(time);
	}

	/**
	 * 寰楀埌鐭棩鏈熸牸寮忓瓧涓�
	 *
	 * @param date
	 * @return
	 */
	public static String getShortDateStr(Date date) {
		return SHORT_DATE_FORMAT.format(date);
	}

	public static String getShortDateStr() {
		return SHORT_DATE_FORMAT.format(new Date());
	}

	/**
	 * 璁＄畻 second 绉掑悗鐨勬椂闂�
	 *
	 * @param date
	 * @param second
	 * @return
	 */
	public static Date addSecond(Date date, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		;
		calendar.add(Calendar.SECOND, second);
		return calendar.getTime();
	}

	/**
	 * 璁＄畻 minute 鍒嗛挓鍚庣殑鏃堕棿
	 *
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date addMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	/**
	 * 璁＄畻 hour 灏忔椂鍚庣殑鏃堕棿
	 *
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hour);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌day鐨勮捣濮嬫椂闂寸偣銆�
	 *
	 * @param date
	 * @return
	 */
	public static Date getDayStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌day鐨勭粓姝㈡椂闂寸偣.
	 *
	 * @param date
	 * @return
	 */
	public static Date getDayEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MILLISECOND, -1);
		return calendar.getTime();
	}

	/**
	 * 璁＄畻 day 澶╁悗鐨勬椂闂�
	 *
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌month鐨勭粓姝㈡椂闂寸偣.
	 *
	 * @param date
	 * @return
	 */
	public static Date getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.MILLISECOND, -1);
		return calendar.getTime();
	}

	public static Date addYear(Date date, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, 365 * year);
		return calendar.getTime();
	}

	public static Timestamp strToTimestamp(String dateStr) {
		return Timestamp.valueOf(dateStr);
	}

	public static Timestamp strToTimestamp(Date date) {
		return Timestamp.valueOf(formatTimestamp.format(date));
	}

	public static Timestamp getCurTimestamp() {
		return Timestamp.valueOf(formatTimestamp.format(new Date()));
	}

	/**
	 * 鍙栧緱涓や釜鏃ユ湡涔嬮棿鐨勬棩鏁�
	 *
	 * @return t1鍒皌2闂寸殑鏃ユ暟锛屽鏋渢2 鍦� t1涔嬪悗锛岃繑鍥炴鏁帮紝鍚﹀垯杩斿洖璐熸暟
	 */
	public static long daysBetween(java.sql.Timestamp t1, java.sql.Timestamp t2) {
		return (t2.getTime() - t1.getTime()) / DAY_MILLI;
	}

	/**
	 * 杩斿洖java.sql.Timestamp鍨嬬殑SYSDATE
	 *
	 * @return java.sql.Timestamp鍨嬬殑SYSDATE
	 * @since 1.0
	 * @history
	 */
	public static java.sql.Timestamp getSysDateTimestamp() {
		return new java.sql.Timestamp(System.currentTimeMillis());
	}

	/**
	 * 鍒╃敤缂虹渷鐨凞ate鏍煎紡(YYYY/MM/DD)杞崲String鍒癹ava.sql.Timestamp
	 *
	 * @param sDate
	 *            Date string
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static java.sql.Timestamp toSqlTimestamp(String sDate) {
		if (sDate == null) {
			return null;
		}
		if (sDate.length() != DateUtils.DATE_FORMAT_DATEONLY.length()) {
			return null;
		}
		return toSqlTimestamp(sDate, DateUtils.DATE_FORMAT_DATEONLY);
	}

	/**
	 * 鍒╃敤缂虹渷鐨凞ate鏍煎紡(YYYY/MM/DD hh:mm:ss)杞寲String鍒癹ava.sql.Timestamp
	 *
	 * @param sDate
	 *            Date string
	 * @param sFmt
	 *            Date format DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static java.sql.Timestamp toSqlTimestamp(String sDate, String sFmt) {
		String temp = null;
		if (sDate == null || sFmt == null) {
			return null;
		}
		if (sDate.length() != sFmt.length()) {
			return null;
		}
		if (sFmt.equals(DateUtils.DATE_FORMAT_DATETIME)) {
			temp = sDate.replace('/', '-');
			temp = temp + ".000000000";
		} else if (sFmt.equals(DateUtils.DATE_FORMAT_DATEONLY)) {
			temp = sDate.replace('/', '-');
			temp = temp + " 00:00:00.000000000";
			// }else if( sFmt.equals (DateUtils.DATE_FORMAT_SESSION )){
			// //Format: 200009301230
			// temp =
			// sDate.substring(0,4)+"-"+sDate.substring(4,6)+"-"+sDate.substring(6,8);
			// temp += " " + sDate.substring(8,10) + ":" +
			// sDate.substring(10,12) + ":00.000000000";
		} else {
			return null;
		}
		// java.sql.Timestamp.value() 瑕佹眰鐨勬牸寮忓繀椤讳负yyyy-mm-dd hh:mm:ss.fffffffff
		return java.sql.Timestamp.valueOf(temp);
	}

	/**
	 * 浠YYY/MM/DD HH24:MI:SS鏍煎紡杩斿洖绯荤粺鏃ユ湡鏃堕棿
	 *
	 * @return 绯荤粺鏃ユ湡鏃堕棿
	 * @since 1.0
	 * @history
	 */
	public static String getSysDateTimeString() {
		return toString(new java.util.Date(System.currentTimeMillis()), DateUtils.sdfDateTime);
	}

	/**
	 * 鏍规嵁鎸囧畾鐨凢ormat杞寲java.util.Date鍒癝tring
	 *
	 * @param dt
	 *            java.util.Date instance
	 * @param sFmt
	 *            Date format , DATE_FORMAT_DATEONLY or DATE_FORMAT_DATETIME
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toString(java.util.Date dt, String sFmt) {
		if (dt == null || sFmt == null || "".equals(sFmt)) {
			return "";
		}
		return toString(dt, new SimpleDateFormat(sFmt));
	}

	/**
	 * 鍒╃敤鎸囧畾SimpleDateFormat instance杞崲java.util.Date鍒癝tring
	 *
	 * @param dt
	 *            java.util.Date instance
	 * @param formatter
	 *            SimpleDateFormat Instance
	 * @return
	 * @since 1.0
	 * @history
	 */
	private static String toString(java.util.Date dt, SimpleDateFormat formatter) {
		String sRet = null;

		try {
			sRet = formatter.format(dt).toString();
		} catch (Exception e) {
//			logger.error(e);
			sRet = null;
		}

		return sRet;
	}

	/**
	 * 杞崲java.sql.Timestamp鍒癝tring锛屾牸寮忎负YYYY/MM/DD HH24:MI
	 *
	 * @param dt
	 *            java.sql.Timestamp instance
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toSqlTimestampString2(java.sql.Timestamp dt) {
		if (dt == null) {
			return null;
		}
		String temp = toSqlTimestampString(dt, DateUtils.DATE_FORMAT_DATETIME);
		return temp.substring(0, 16);
	}

	public static String toString(java.sql.Timestamp dt) {
		return dt == null ? "" : toSqlTimestampString2(dt);
	}

	/**
	 * 鏍规嵁鎸囧畾鐨勬牸寮忚浆鎹ava.sql.Timestamp鍒癝tring
	 *
	 * @param dt
	 *            java.sql.Timestamp instance
	 * @param sFmt
	 *            Date 鏍煎紡锛孌ATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME/
	 *            DATE_FORMAT_SESSION
	 * @return
	 * @since 1.0
	 * @history
	 */
	public static String toSqlTimestampString(java.sql.Timestamp dt, String sFmt) {
		String temp = null;
		String out = null;
		if (dt == null || sFmt == null) {
			return null;
		}
		temp = dt.toString();
		if (sFmt.equals(DateUtils.DATE_FORMAT_DATETIME) || // "YYYY/MM/DD
				// HH24:MI:SS"
				sFmt.equals(DateUtils.DATE_FORMAT_DATEONLY)) { // YYYY/MM/DD
			temp = temp.substring(0, sFmt.length());
			out = temp.replace('/', '-');
			// }else if( sFmt.equals (DateUtils.DATE_FORMAT_SESSION ) ){
			// //Session
			// out =
			// temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10);
			// out += temp.substring(12,14) + temp.substring(15,17);
		}
		return out;
	}

	// 寰楀埌褰撳墠鏃ユ湡鐨勬槦鏈�
	public static int getWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int w = cal.get(Calendar.DAY_OF_WEEK);
		return w;
	}

	/**
	 * Timestamp 鏍煎紡杞崲鎴恲yyy-MM-dd timestampToSql(Timestamp 鏍煎紡杞崲鎴恲yyy-MM-dd)
	 *
	 * @param timestamp
	 *            鏃堕棿
	 * @return createTimeStr yyyy-MM-dd 鏃堕棿
	 * @Exception 寮傚父瀵硅薄
	 * @since V1.0
	 */
	public static String timestampToStringYMD(java.sql.Timestamp timestamp) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DATEONLY);
		String createTimeStr = sdf.format(timestamp);
		return createTimeStr;
	}

	/**
	 * 鍒ゆ柇涓�涓椂闂存槸鍚﹀湪鏌愪釜鏃堕棿鍖洪棿鍐�
	 *
	 * @param now
	 *            鐩爣鏃堕棿
	 * @param start
	 *            鏃堕棿鍖洪棿寮�濮�
	 * @param end
	 *            鏃堕棿鍖洪棿缁撴潫
	 * @param model
	 *            鍖洪棿妯″紡
	 * @return 鏄惁鍦ㄥ尯闂村唴
	 */
	public static boolean isBetween(Date now, Date start, Date end, int model) {
		return isBetween(now, start, end, model, LEFT_OPEN_RIGHT_OPEN);
	}

	/**
	 * 鍒ゆ柇鏃堕棿鏄惁鍦ㄥ埗瀹氱殑鏃堕棿娈典箣绫�
	 *
	 * @param date
	 *            闇�瑕佸垽鏂殑鏃堕棿
	 * @param start
	 *            鏃堕棿娈电殑璧峰鏃堕棿
	 * @param end
	 *            鏃堕棿娈电殑鎴鏃堕棿
	 * @param interModel
	 *            鍖洪棿鐨勬ā寮�
	 *
	 *            <pre>
	 * 		鍙栧�硷細
	 * 			LEFT_OPEN_RIGHT_OPEN
	 * 			LEFT_CLOSE_RIGHT_OPEN
	 * 			LEFT_OPEN_RIGHT_CLOSE
	 * 			LEFT_CLOSE_RIGHT_CLOSE
	 * </pre>
	 * @param compModel
	 *            姣旇緝鐨勬ā寮�
	 *
	 *            <pre>
	 * 		鍙栧�硷細
	 * 			COMP_MODEL_DATE		鍙瘮杈冩棩鏈燂紝涓嶆瘮杈冩椂闂�
	 * 			COMP_MODEL_TIME		鍙瘮杈冩椂闂达紝涓嶆瘮杈冩棩鏈�
	 * 			COMP_MODEL_DATETIME 姣旇緝鏃ユ湡锛屼篃姣旇緝鏃堕棿
	 * </pre>
	 * @return
	 */
	public static boolean isBetween(Date date, Date start, Date end, int interModel, int compModel) {
		if (date == null || start == null || end == null) {
			throw new IllegalArgumentException("鏃ユ湡涓嶈兘涓虹┖");
		}
		SimpleDateFormat format = null;
		switch (compModel) {
			case COMP_MODEL_DATE: {
				format = new SimpleDateFormat("yyyyMMdd");
				break;
			}
			case COMP_MODEL_TIME: {
				format = new SimpleDateFormat("HHmmss");
				break;
			}
			case COMP_MODEL_DATETIME: {
				format = new SimpleDateFormat("yyyyMMddHHmmss");
				break;
			}
			default: {
				throw new IllegalArgumentException(String.format("鏃ユ湡鐨勬瘮杈冩ā寮廩%d]鏈夎", compModel));
			}
		}
		long dateNumber = Long.parseLong(format.format(date));
		long startNumber = Long.parseLong(format.format(start));
		long endNumber = Long.parseLong(format.format(end));
		switch (interModel) {
			case LEFT_OPEN_RIGHT_OPEN: {
				if (dateNumber <= startNumber || dateNumber >= endNumber) {
					return false;
				} else {
					return true;
				}
			}
			case LEFT_CLOSE_RIGHT_OPEN: {
				if (dateNumber < startNumber || dateNumber >= endNumber) {
					return false;
				} else {
					return true;
				}
			}
			case LEFT_OPEN_RIGHT_CLOSE: {
				if (dateNumber <= startNumber || dateNumber > endNumber) {
					return false;
				} else {
					return true;
				}
			}
			case LEFT_CLOSE_RIGHT_CLOSE: {
				if (dateNumber < startNumber || dateNumber > endNumber) {
					return false;
				} else {
					return true;
				}
			}
			default: {
				throw new IllegalArgumentException(String.format("鏃ユ湡鐨勫尯闂存ā寮廩%d]鏈夎", interModel));
			}
		}
	}

	/**
	 * 寰楀埌褰撳墠鍛ㄨ捣濮嬫椂闂�
	 *
	 * @param date
	 * @return
	 */
	public static Date getWeekStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.get(Calendar.WEEK_OF_YEAR);
		int firstDay = calendar.getFirstDayOfWeek();
		calendar.set(Calendar.DAY_OF_WEEK, firstDay);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌褰撳墠鍛ㄦ埅姝㈡椂闂�
	 *
	 * @param date
	 * @return
	 */
	public static Date getWeekEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.get(Calendar.WEEK_OF_YEAR);
		int firstDay = calendar.getFirstDayOfWeek();
		calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌褰撴湀璧峰鏃堕棿
	 *
	 * @param date
	 * @return
	 */
	public static Date getMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌褰撳墠骞磋捣濮嬫椂闂�
	 *
	 * @param date
	 * @return
	 */
	public static Date getYearStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 寰楀埌褰撳墠骞存渶鍚庝竴澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getYearEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		calendar.set(Calendar.MONTH, 11);
		calendar.set(Calendar.DAY_OF_MONTH, 31);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 鍙栧緱鏈堝ぉ鏁�
	 *
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 鍙栧緱鏈堢涓�澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 鍙栧緱鏈堟渶鍚庝竴澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 鍙栧緱瀛ｅ害绗竴澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getSeasonStart(Date date) {
		return getDayStart(getFirstDateOfMonth(getSeasonDate(date)[0]));
	}

	/**
	 * 鍙栧緱瀛ｅ害鏈�鍚庝竴澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getSeasonEnd(Date date) {
		return getDayEnd(getLastDateOfMonth(getSeasonDate(date)[2]));
	}

	/**
	 * 鍙栧緱瀛ｅ害鏈�
	 *
	 * @param date
	 * @return
	 */
	public static Date[] getSeasonDate(Date date) {
		Date[] season = new Date[3];

		Calendar c = Calendar.getInstance();
		c.setTime(date);

		int nSeason = getSeason(date);
		if (nSeason == 1) {// 绗竴瀛ｅ害
			c.set(Calendar.MONTH, Calendar.JANUARY);
			season[0] = c.getTime();
			c.set(Calendar.MONTH, Calendar.FEBRUARY);
			season[1] = c.getTime();
			c.set(Calendar.MONTH, Calendar.MARCH);
			season[2] = c.getTime();
		} else if (nSeason == 2) {// 绗簩瀛ｅ害
			c.set(Calendar.MONTH, Calendar.APRIL);
			season[0] = c.getTime();
			c.set(Calendar.MONTH, Calendar.MAY);
			season[1] = c.getTime();
			c.set(Calendar.MONTH, Calendar.JUNE);
			season[2] = c.getTime();
		} else if (nSeason == 3) {// 绗笁瀛ｅ害
			c.set(Calendar.MONTH, Calendar.JULY);
			season[0] = c.getTime();
			c.set(Calendar.MONTH, Calendar.AUGUST);
			season[1] = c.getTime();
			c.set(Calendar.MONTH, Calendar.SEPTEMBER);
			season[2] = c.getTime();
		} else if (nSeason == 4) {// 绗洓瀛ｅ害
			c.set(Calendar.MONTH, Calendar.OCTOBER);
			season[0] = c.getTime();
			c.set(Calendar.MONTH, Calendar.NOVEMBER);
			season[1] = c.getTime();
			c.set(Calendar.MONTH, Calendar.DECEMBER);
			season[2] = c.getTime();
		}
		return season;
	}

	/**
	 *
	 * 1 绗竴瀛ｅ害 2 绗簩瀛ｅ害 3 绗笁瀛ｅ害 4 绗洓瀛ｅ害
	 *
	 * @param date
	 * @return
	 */
	public static int getSeason(Date date) {

		int season = 0;

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		switch (month) {
			case Calendar.JANUARY:
			case Calendar.FEBRUARY:
			case Calendar.MARCH:
				season = 1;
				break;
			case Calendar.APRIL:
			case Calendar.MAY:
			case Calendar.JUNE:
				season = 2;
				break;
			case Calendar.JULY:
			case Calendar.AUGUST:
			case Calendar.SEPTEMBER:
				season = 3;
				break;
			case Calendar.OCTOBER:
			case Calendar.NOVEMBER:
			case Calendar.DECEMBER:
				season = 4;
				break;
			default:
				break;
		}
		return season;
	}

	/**
	 * 瀛楃涓茶浆date
	 *
	 * @param dateString
	 * @return
	 */
	public static Date StringToDate(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
//			logger.error(e);
		}
		return date;
	}

	/**
	 * 鍒ゆ柇杈撳叆鏃ユ湡鏄竴涓槦鏈熶腑鐨勭鍑犲ぉ(鏄熸湡澶╀负涓�涓槦鏈熺涓�澶�)
	 *
	 * @param date
	 * @return
	 */
	public static int getWeekIndex(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 褰撳墠鏃堕棿鐨勫墠鍑犲ぉ锛屽苟涓斾互渚嬪2013/12/09 00:00:00 褰㈠紡杈撳嚭
	 */
	public static Date subDays(int days) {
		Date date = addDay(new Date(), -days);
		String dateStr = getReqDate(date);
		Date date1 = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date1 = sdf.parse(dateStr);
		} catch (ParseException e) {
//			logger.error(e);
		}
		return date1;
	}

	/**
	 * 鍒ゆ柇寮�濮嬫椂闂村拰缁撴潫鏃堕棿锛屾槸鍚﹁秴鍑轰簡褰撳墠鏃堕棿鐨勪竴瀹氱殑闂撮殧鏁伴檺鍒� 濡傦細寮�濮嬫椂闂村拰缁撴潫鏃堕棿锛屼笉鑳借秴鍑鸿窛绂诲綋鍓嶆椂闂�90澶�
	 *
	 * @param startDate
	 *            寮�濮嬫椂闂�
	 * @param endDate
	 *            缁撴潫鏃堕棿鎸�
	 * @param interval
	 *            闂撮殧鏁�
	 * @param dateUnit
	 *            鍗曚綅(濡傦細鏈堬紝鏃�),鍙傜収Calendar鐨勬椂闂村崟浣�
	 * @return
	 */
	public static boolean isOverIntervalLimit(Date startDate, Date endDate, int interval, int dateUnit) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(dateUnit, interval * (-1));
		Date curDate = getDayStart(cal.getTime());
		if (getDayStart(startDate).compareTo(curDate) < 0 || getDayStart(endDate).compareTo(curDate) < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 鍒ゆ柇寮�濮嬫椂闂村拰缁撴潫鏃堕棿锛屾槸鍚﹁秴鍑轰簡褰撳墠鏃堕棿鐨勪竴瀹氱殑闂撮殧鏁伴檺鍒�, 鏃堕棿鍗曚綅榛樿涓哄ぉ鏁� 濡傦細寮�濮嬫椂闂村拰缁撴潫鏃堕棿锛屼笉鑳借秴鍑鸿窛绂诲綋鍓嶆椂闂�90澶�
	 *
	 * @param startDate
	 *            寮�濮嬫椂闂�
	 * @param endDate
	 *            缁撴潫鏃堕棿鎸�
	 * @param interval
	 *            闂撮殧鏁�
	 * @return
	 */
	public static boolean isOverIntervalLimit(Date startDate, Date endDate, int interval) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_MONTH, interval * (-1));
		Date curDate = getDayStart(cal.getTime());
		if (getDayStart(startDate).compareTo(curDate) < 0 || getDayStart(endDate).compareTo(curDate) < 0) {
			return true;
		}
		return false;
	}

	/**
	 * 浼犲叆鏃堕棿瀛楃涓插強鏃堕棿鏍煎紡锛岃繑鍥炲搴旂殑Date瀵硅薄
	 *
	 * @param src
	 *            鏃堕棿瀛楃涓�
	 * @param pattern
	 *            鏃堕棿鏍煎紡
	 * @return Date
	 */
	public static java.util.Date getDateFromString(String src, String pattern) {
		SimpleDateFormat f = new SimpleDateFormat(pattern);
		try {
			return f.parse(src);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 鍙栧搴�
	 *
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getQuarter(Date date) {
		if (date.getMonth() == 0 || date.getMonth() == 1 || date.getMonth() == 2) {
			return 1;
		} else if (date.getMonth() == 3 || date.getMonth() == 4 || date.getMonth() == 5) {
			return 2;
		} else if (date.getMonth() == 6 || date.getMonth() == 7 || date.getMonth() == 8) {
			return 3;
		} else if (date.getMonth() == 9 || date.getMonth() == 10 || date.getMonth() == 11) {
			return 4;
		} else {
			return 0;

		}
	}

	/**
	 * 鍙栧緱閫氱敤鏃ユ湡鏃堕棿鏍煎紡瀛楃涓�
	 *
	 * @param Date
	 * @return String
	 */
	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(date);
	}

	/**
	 * 鑾峰彇褰撴棩鐨勬棩鏈熸牸寮忎覆
	 *
	 * @param
	 * @return String
	 */
	public static String today() {
		return formatDate(new Date(), "yyyy-MM-dd");
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿鏍煎紡涓�
	 *
	 * @param
	 * @return String
	 */
	public static String currentTime() {
		return formatDate(new Date(), "yyyyMMddhhmmssSSS");
	}

	/**
	 * 鍙栧緱鎸囧畾鏃ユ湡鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param Date
	 * @return String
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	/**
	 * 鑾峰彇鏄ㄦ棩鐨勬棩鏈熸牸寮忎覆
	 *
	 * @return Date
	 */
	public static String getYesterday() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		return formatDate(calendar.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 鍒ゆ柇褰撳墠鏃堕棿鏄惁鍦ㄤ竴瀹氱殑鏃堕棿鑼冨洿鍐�
	 *
	 * @param String
	 * @return boolean
	 */
	public static boolean isInBetweenTimes(String startTime, String endTime) {
		Date nowTime = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String time = sdf.format(nowTime);
		if (time.compareTo(startTime) >= 0 && time.compareTo(endTime) <= 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 瀛楃杞棩鏈�
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date getDateByStr(String dateStr) {
		SimpleDateFormat formatter = null;
		if (dateStr == null) {
			return null;
		} else if (dateStr.length() == 10) {
			formatter = new SimpleDateFormat("yyyy-MM-dd");
		} else if (dateStr.length() == 16) {
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		} else if (dateStr.length() == 19) {
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if (dateStr.length() > 19) {
			dateStr = dateStr.substring(0, 19);
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else {
			return null;
		}
		try {
			return formatter.parse(dateStr);
		} catch (ParseException e) {
//			logger.error(e);
			return null;
		}
	}

	/**
	 * 鏍规嵁浼犲叆鐨勬暟瀛楋紝杈撳嚭鐩告瘮鐜板湪days澶╃殑鏁版嵁
	 *
	 * @param days
	 * @return Date
	 */
	public static Date getDate(int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	/**
	 * 鏃ユ湡鏈�澶ф椂闂�
	 *
	 * @param dt
	 * @return
	 */
	public static Date getMaxTime(Date dt) {

		Date dt1 = null;
		Calendar ca = Calendar.getInstance();
		ca.setTime(dt);
		ca.add(Calendar.DAY_OF_MONTH, 1);
		dt1 = ca.getTime();
		dt1 = DateUtils.getMinTime(dt1);
		ca.setTime(dt1);
		ca.add(Calendar.SECOND, -1);
		dt1 = ca.getTime();
		return dt1;
	}

	/**
	 * 鏃ユ湡鏈�灏忔椂闂�
	 *
	 * @param dt
	 * @return
	 */
	public static Date getMinTime(Date dt) {
		Date dt1 = null;
		dt1 = DateUtils.getDateByStr(DateUtils.formatDate(dt, "yyyy-MM-dd"));
		return dt1;
	}

	/**
	 * 鏈堢殑鏈�鍚庝竴澶�
	 *
	 * @param sDate1
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date getLastDayOfMonth(Date date) {
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(date);
		int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
		Date lastDate = cDay1.getTime();
		lastDate.setDate(lastDay);
		return lastDate;
	}

	/**
	 * 鏈堢殑绗竴澶�
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		return calendar.getTime();
	}

	/**
	 * 涓婃湀绗竴澶�
	 *
	 * @return
	 */
	public static Date getPreviousMonthFirstDay() {
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙�
		lastDate.add(Calendar.MONTH, -1);// 鍑忎竴涓湀锛屽彉涓轰笅鏈堢殑1鍙�
		return getMinTime(lastDate.getTime());
	}

	/**
	 * 涓婃湀鏈�鍚庝竴澶�
	 *
	 * @return
	 */
	public static Date getPreviousMonthLastDay() {
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 璁句负褰撳墠鏈堢殑1鍙�
		lastDate.add(Calendar.DATE, -1);
		return getMinTime(lastDate.getTime());
	}

	/**
	 * 涓や釜鏃ユ湡鐩稿叧澶╂暟
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long getDateDiff(String startDate, String endDate) {
		long diff = 0;
		try {
			Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
			Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);

			diff = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0 ? (date1.getTime() - date2.getTime())
					/ (24 * 60 * 60 * 1000)
					: (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
		}
		return diff;
	}


	/**
	 * getStrFormTime: <br/>
	 *
	 * @param form
	 *            鏍煎紡鏃堕棿
	 * @param date
	 *            鏃堕棿
	 * @return
	 */
	public static String getStrFormTime(String form, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(form);
		return sdf.format(date);
	}

	/**
	 * 鑾峰彇鍑犲ぉ鍐呮棩鏈� return 2014-5-4銆�2014-5-3
	 */
	public static List<String> getLastDays(int countDay) {
		List<String> listDate = new ArrayList<String>();
		for (int i = 0; i < countDay; i++) {
			listDate.add(DateUtils.getReqDateyyyyMMdd(DateUtils.getDate(-i)));
		}
		return listDate;
	}

	/**
	 * 瀵规椂闂磋繘琛屾牸寮忓寲
	 *
	 * @param date
	 * @return
	 */
	public static Date dateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date value = new Date();

		try {
			value = sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return value;

	}

	/**
	 * 寰楀埌鏈棩鐨勫墠鍚庡嚑涓湀鏃堕棿
	 */
	public static String getDateBeforeMonth(int number) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, number);
		return getDateFormat(cal.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 鑾峰彇鎸囧畾鏃ユ湡鍓嶅悗鍑犱釜鏈堟椂闂�
	 *
	 * @param date
	 * @param number
	 * @return
	 */
	public static String getDateBeforeMonth(Date date, int number) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, number);
		return getDateFormat(cal.getTime(), "yyyy-MM-dd");
	}

	/**
	 * 瀛楃杞寲鏃堕棿
	 *
	 * @param str
	 * @return
	 */
	public static Date StringToDay(String str) {
		String format = "yyyy-MM-dd";
		return StringToDate(str, format);
	}

	/**
	 * 瀛楃杞寲鏃堕棿
	 *
	 * @param str
	 * @return
	 */
	public static Date StringToTime(String str) {
		String format = "yyyy-MM-dd HH:mm:ss";
		return StringToDate(str, format);
	}

	/**
	 * 瀛楃涓茶浆鍖栨椂闂�
	 *
	 * @param str
	 * @return
	 */
	public static Date StringToDate(String str, String format) {
		SimpleDateFormat sbf = new SimpleDateFormat(format);
		try {
			return sbf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 鑾峰彇鏈夋牸寮忕殑褰撳墠鏃堕棿
	 *
	 * @param format
	 * @return
	 */
	public static String getNow(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 鏃堕棿杞寲
	 *
	 * @param data
	 * @param format
	 * @return
	 */
	public static String getDateFormat(Date data, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(data);
	}

	/**
	 * 鏃堕棿姣旇緝
	 *
	 * @param max
	 * @param min
	 * @return
	 */
	public static boolean isBefore(String max, String min) {
		DateFormat df = DateFormat.getDateInstance();
		try {
			Date maxDate = df.parse(max);
			Date minDate = df.parse(min);
			return maxDate.before(minDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 鏃堕棿姣旇緝
	 *
	 * @param max
	 * @param min
	 * @return
	 */
	public static boolean isAfter(String max, String min) {
		DateFormat df = DateFormat.getDateInstance();
		try {
			Date maxDate = df.parse(max);
			Date minDate = df.parse(min);
			return maxDate.after(minDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿鍓嶅悗 濡�-5 +5
	 *
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getTime(String format, int days) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		now.add(Calendar.DATE, days);
		return formatter.format(now.getTime());
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿鍓嶅悗 濡�-5 +5
	 *
	 * @param format
	 * @param days
	 * @return
	 */
	public static Date getTime2(String format, int days) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.DATE, days);
		return now.getTime();
	}

	/**
	 * 鎸囧畾鏃堕棿鍔爊绉掞紝鍙负璐熸暟
	 *
	 * @return
	 */
	public static Date dateAddSec(Date date, int sec) {
		if (date == null) {
			return null;
		}
		long time = sec * 1000;
		long currentTime = date.getTime() + time;
		return new Date(currentTime);
	}

	/**
	 * 鑾峰彇鎸囧畾鏃堕棿鍓嶅悗 濡�-5 +5
	 *
	 * @param time
	 * @param format
	 * @param days
	 * @return
	 */
	public static String getTime(String time, String format, int days) {
		Calendar now = Calendar.getInstance();
		DateFormat df = DateFormat.getDateInstance();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			now.setTime(df.parse(time));
			now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return formatter.format(now.getTime());
	}

	/**
	 * 鑾峰彇鎸囧畾鏃堕棿鍓嶅悗 濡�-5 +5
	 *
	 * @param time
	 * @param format
	 * @param days
	 * @return
	 */
	public static Date getTime(Date date, int days) {
		Calendar now = Calendar.getInstance();
		try {
			now.setTime(date);
			now.set(Calendar.DATE, now.get(Calendar.DATE) + days);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return now.getTime();
	}

	/**
	 * 鑾峰緱褰撳ぉ鏃堕棿鐨�0鐐�
	 *
	 * @author Kee.Li
	 *
	 * @return
	 */
	public static Date getCurrentDayZeroTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 鑾峰緱褰撳ぉ鏃堕棿鐨�0鐐�
	 *
	 * @author Kee.Li
	 *
	 * @return
	 */
	public static Date getDateZeroTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 灏哾ate杞负yyyy-MM-dd HH:mm:ss鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param date
	 * @return
	 */
	public static String dateToTime(Date date) {
		String format = "yyyy-MM-dd HH:mm:ss";
		return getDateFormat(date, format);
	}

	/**
	 * 灏哾ate杞负yyyyMMddHHmmssSSS鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param date
	 * @return
	 */
	public static String dateToTimeMMss(Date date) {
		String format = "yyyyMMddHHmmssSSS";
		return getDateFormat(date, format);
	}
	/**
	 * 灏哾ate杞负yyyyMMddHHmmss鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param date
	 * @return
	 */
	public static String dateToTimeMM(Date date) {
		String format = "yyyyMMddHHmmss";
		return getDateFormat(date, format);
	}
	/**
	 * 灏哾ate杞负yyyy-MM-dd鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param date
	 * @return
	 */
	public static String dateToDay(Date date) {
		if (date == null) {
			return null;
		}
		String format = "yyyy-MM-dd";
		return getDateFormat(date, format);
	}

	/**
	 * 灏哾ate杞负yyyy-MM-dd HH:mm鏍煎紡鐨勫瓧绗︿覆
	 *
	 * @param date
	 * @return
	 */
	public static String dateToMinute(Date date) {
		if (date == null) {
			return "";
		}
		String format = "yyyy-MM-dd HH:mm";
		return getDateFormat(date, format);
	}

	public static String dateToPlusDay(Date date) {

		Date nextDay = new Date(date.getTime() + 24 * 60 * 60 * 1000);

		return dateToDay(nextDay);
	}

	/**
	 * 璁＄畻骞撮緞
	 *
	 * @param time
	 */
	public static int getYear(String time) {
		Date cals = null;
		try {
			cals = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

		Calendar cal = Calendar.getInstance();

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1; // 娉ㄦ剰姝ゅ锛屽鏋滀笉鍔�1鐨勮瘽璁＄畻缁撴灉鏄敊璇殑
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(cals);

		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				} else {
					// do nothing
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}

		return age <= 0 ? 0 : age;
	}

	/**
	 * 寰楀埌鏄庡ぉ鐨勬棩鏈�
	 *
	 * @return
	 */
	public static Date getNextDat() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 鏃堕棿鍔爊鍒嗛挓
	 *
	 * @return
	 */
	public static Date dateAddMin(Date date, int min) {
		if (date == null) {
			return null;
		}
		long time = min * 60 * 1000;
		long currentTime = date.getTime() + time;
		return new Date(currentTime);
	}

	/**
	 * 鑾峰彇鎸囧畾鏃堕棿鏄槦鏈熷嚑銆� 鏄熸湡澶╀负0銆傛槦鏈熶竴涓�:1
	 *
	 * @param date
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		return ca.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 鑾峰彇鎸囧畾鏃堕棿鏄槦鏈熷嚑銆� 鏄熸湡澶╀负0銆傛槦鏈熶竴涓�:1
	 *
	 * @param date
	 * @return
	 */
	public static int getWeek(String str) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(StringToDay(str));
		return ca.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 鏍规嵁鎸囧畾鏃ユ湡鍒ゆ柇灞炰簬鏄熸湡鍑犮��
	 *
	 * @return
	 */
	public static String getWeekToString(String str) {
		int week = getWeek(str);
		switch (week) {
			case 0:
				return "鍛ㄦ棩";
			case 1:
				return "鍛ㄤ竴";
			case 2:
				return "鍛ㄤ簩";
			case 3:
				return "鍛ㄤ笁";
			case 4:
				return "鍛ㄥ洓";
			case 5:
				return "鍛ㄤ簲";
			case 6:
				return "鍛ㄥ叚";
		}
		return "";
	}

	/**
	 * 鏍规嵁骞� 鏈� 鑾峰彇瀵瑰簲鐨勬湀浠� 澶╂暟
	 * */
	public static int getDaysByYearMonth(Date date) {
		Calendar a = Calendar.getInstance();
		a.setTime(date);
		Integer year = a.get(Calendar.YEAR);
		Integer month = a.get(Calendar.MONTH) + 1;
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	/**
	 * 璁＄畻涓や釜鏃ユ湡涔嬮棿鐩稿樊鐨勫ぉ鏁�
	 *
	 * @param smdate
	 *            杈冨皬鐨勬椂闂�
	 * @param bdate
	 *            杈冨ぇ鐨勬椂闂�
	 * @return 鐩稿樊澶╂暟
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 瀛楃涓茬殑鏃ユ湡鏍煎紡鐨勮绠�
	 */
	public static int daysBetween(String smdate, String bdate, String format)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 鏃堕棿鎴宠浆鏃堕棿锛屽井淇′笂浼犲濯掍綋鍚庤繑鍥炵殑鏃堕棿鎴筹紝鍦ㄨ皟鐢ㄦ鏂规硶鏃堕渶*1000
	 *
	 * @param stamp
	 *            鏃堕棿鎴�
	 * @return
	 */
	public static Date stampToDate(String stamp) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long time = new Long(stamp);
		String d = format.format(time);
		Date date = null;
		try {
			date = format.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 鑾峰彇褰撳墠鏃堕棿鐨勫勾浠�
	 *
	 * @param stamp
	 *            鏃堕棿鎴�
	 * @return
	 */
	public static Integer getYear() {
		Calendar now = Calendar.getInstance();
		Integer year = now.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 涓や釜鏃堕棿涔嬮棿鐩稿樊璺濈澶氬皯澶�
	 *
	 * @param one
	 *            鏃堕棿鍙傛暟 1锛�
	 * @param two
	 *            鏃堕棿鍙傛暟 2锛�
	 * @return 鐩稿樊澶╂暟
	 */
	public static long getDistanceDays(String str1, String str2)
			throws Exception {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date one;
		Date two;
		long days = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			days = diff / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}

	/**
	 * 涓や釜鏃堕棿鐩稿樊璺濈澶氬皯澶╁灏戝皬鏃跺灏戝垎澶氬皯绉�
	 *
	 * @param str1
	 *            鏃堕棿鍙傛暟 1 鏍煎紡锛�1990-01-01 12:00:00
	 * @param str2
	 *            鏃堕棿鍙傛暟 2 鏍煎紡锛�2009-01-01 12:00:00
	 * @return long[] 杩斿洖鍊间负锛歿澶�, 鏃�, 鍒�, 绉拀
	 */
	public static long[] getDistanceTimes(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long[] times = { day, hour, min, sec };
		return times;
	}

	/**
	 * 涓や釜鏃堕棿鐩稿樊璺濈澶氬皯澶╁灏戝皬鏃跺灏戝垎澶氬皯绉�
	 *
	 * @param str1
	 *            鏃堕棿鍙傛暟 1 鏍煎紡锛�1990-01-01 12:00:00
	 * @param str2
	 *            鏃堕棿鍙傛暟 2 鏍煎紡锛�2009-01-01 12:00:00
	 * @return String 杩斿洖鍊间负锛歺x澶﹛x灏忔椂xx鍒唜x绉�
	 */
	public static String getDistanceTime(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				// diff = time1 - time2;
				return "00澶�00灏忔椂00鍒�00绉�";
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer();
		sb.append(!(day >= 10) ? "0" + day + "澶�" : day + "澶�");
		sb.append(!(hour >= 10) ? "0" + hour + "灏忔椂" : hour + "灏忔椂");
		sb.append(!(min >= 10) ? "0" + min + "鍒�" : min + "鍒�");
		sb.append(!(sec >= 10) ? "0" + sec + "绉�" : sec + "绉�");
		return sb.toString();
		// return day + "澶�" + hour + "灏忔椂" + min + "鍒�" + sec + "绉�";
	}

	/*
	 * 鍒ゆ柇涓や釜鏃堕棿鏄笉鏄湪涓�涓懆涓�
	 */
	public static boolean isSameWeekWithToday(Date date) {

		if (date == null) {
			return false;
		}

		// 0.鍏堟妸Date绫诲瀷鐨勫璞¤浆鎹alendar绫诲瀷鐨勫璞�
		Calendar todayCal = Calendar.getInstance();
		Calendar dateCal = Calendar.getInstance();

		todayCal.setTime(new Date());
		dateCal.setTime(date);
		int subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR);
		// subYear==0,璇存槑鏄悓涓�骞�
		if (subYear == 0) {
			if (todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (subYear == 1 && dateCal.get(Calendar.MONTH) == 11 && todayCal.get(Calendar.MONTH) == 0) {
			if (todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (subYear == -1 && todayCal.get(Calendar.MONTH) == 11 && dateCal.get(Calendar.MONTH) == 0) {
			if (todayCal.get(Calendar.WEEK_OF_YEAR) == dateCal.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	/*
	 * 鍒ゆ柇涓�涓椂闂村拰鏄笉鏄粖澶�
	 */
	public static boolean isSameDayWithToday(Date date) {

		if (date == null) {
			return false;
		}

		// 0.鍏堟妸Date绫诲瀷鐨勫璞¤浆鎹alendar绫诲瀷鐨勫璞�
		Calendar todayCal = Calendar.getInstance();
		Calendar dateCal = Calendar.getInstance();

		todayCal.setTime(new Date());
		dateCal.setTime(date);
		int subYear = todayCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR);
		int subMouth = todayCal.get(Calendar.MONTH) - dateCal.get(Calendar.MONTH);
		int subDay = todayCal.get(Calendar.DAY_OF_MONTH) - dateCal.get(Calendar.DAY_OF_MONTH);
		if (subYear == 0 && subMouth == 0 && subDay == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 获取两个时间相差分钟数
	 * @param begin
	 * @param end
	 * @return
	 */
	public static long intervalSecond(Date begin,Date end){
		long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
		return between;
	}

	public static String parseUtcDate(String date,String format) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
		Date d = null;
		try {
			date=date.replace("Z", " UTC");
			d = sdf.parse(date);
			return getDateFormat(d,format);
		} catch (ParseException e) {
			e.printStackTrace();
			return date;
		}

	}


}
